package org.apache.james.mime4j.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class MimeUtil
{
  public static final String ENC_7BIT = "7bit";
  public static final String ENC_8BIT = "8bit";
  public static final String ENC_BASE64 = "base64";
  public static final String ENC_BINARY = "binary";
  public static final String ENC_QUOTED_PRINTABLE = "quoted-printable";
  public static final String MIME_HEADER_CONTENT_DESCRIPTION = "content-description";
  public static final String MIME_HEADER_CONTENT_DISPOSITION = "content-disposition";
  public static final String MIME_HEADER_CONTENT_ID = "content-id";
  public static final String MIME_HEADER_LANGAUGE = "content-language";
  public static final String MIME_HEADER_LOCATION = "content-location";
  public static final String MIME_HEADER_MD5 = "content-md5";
  public static final String MIME_HEADER_MIME_VERSION = "mime-version";
  public static final String PARAM_CREATION_DATE = "creation-date";
  public static final String PARAM_FILENAME = "filename";
  public static final String PARAM_MODIFICATION_DATE = "modification-date";
  public static final String PARAM_READ_DATE = "read-date";
  public static final String PARAM_SIZE = "size";
  static Class class$org$apache$james$mime4j$util$MimeUtil;
  private static int counter;
  private static final Log log;
  private static final Random random;

  static
  {
    Class localClass;
    if (class$org$apache$james$mime4j$util$MimeUtil == null)
    {
      localClass = class$("org.apache.james.mime4j.util.MimeUtil");
      class$org$apache$james$mime4j$util$MimeUtil = localClass;
    }
    while (true)
    {
      log = LogFactory.getLog(localClass);
      random = new Random();
      counter = 0;
      return;
      localClass = class$org$apache$james$mime4j$util$MimeUtil;
    }
  }

  static Class class$(String paramString)
  {
    Class localClass;
    try
    {
      localClass = Class.forName(paramString);
      return localClass;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError().initCause(localClassNotFoundException);
    }
  }

  public static String createUniqueBoundary()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("-=Part.");
    localStringBuffer.append(Integer.toHexString(nextCounterValue()));
    localStringBuffer.append('.');
    localStringBuffer.append(Long.toHexString(random.nextLong()));
    localStringBuffer.append('.');
    localStringBuffer.append(Long.toHexString(System.currentTimeMillis()));
    localStringBuffer.append('.');
    localStringBuffer.append(Long.toHexString(random.nextLong()));
    localStringBuffer.append("=-");
    return localStringBuffer.toString();
  }

  public static String createUniqueMessageId(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer("<Mime4j.");
    localStringBuffer.append(Integer.toHexString(nextCounterValue()));
    localStringBuffer.append('.');
    localStringBuffer.append(Long.toHexString(random.nextLong()));
    localStringBuffer.append('.');
    localStringBuffer.append(Long.toHexString(System.currentTimeMillis()));
    if (paramString != null)
    {
      localStringBuffer.append('@');
      localStringBuffer.append(paramString);
    }
    localStringBuffer.append('>');
    return localStringBuffer.toString();
  }

  public static Map getHeaderParams(String paramString)
  {
    Object localObject1;
    Object localObject2;
    int j;
    Object localObject3;
    int k;
    Object localObject4;
    int l;
    char c1;
    Object localObject5;
    Object localObject6;
    int i1;
    int i2;
    String str1 = paramString.trim();
    CharArrayBuffer localCharArrayBuffer1 = new CharArrayBuffer(128);
    int i = 0;
    if (i < str1.length())
    {
      char c2 = str1.charAt(i);
      if ((c2 == '\r') || (c2 == '\n'));
      while (true)
      {
        while (true)
          ++i;
        localCharArrayBuffer1.append(c2);
      }
    }
    String str2 = localCharArrayBuffer1.toString();
    HashMap localHashMap = new HashMap();
    if (str2.indexOf(";") == -1)
    {
      localObject1 = str2;
      localObject2 = null;
      localHashMap.put("", localObject1);
      if (localObject2 == null)
        break label939;
      char[] arrayOfChar = localObject2.toCharArray();
      CharArrayBuffer localCharArrayBuffer2 = new CharArrayBuffer(64);
      CharArrayBuffer localCharArrayBuffer3 = new CharArrayBuffer(64);
      j = 0;
      localObject3 = localCharArrayBuffer3;
      k = 0;
      localObject4 = localCharArrayBuffer2;
      l = 0;
      if (k >= arrayOfChar.length)
        break label906;
      c1 = arrayOfChar[k];
    }
    switch (j)
    {
    default:
      i2 = l;
      localObject5 = localObject4;
      localObject6 = localObject3;
      i1 = j;
    case 99:
    case 0:
      while (true)
      {
        while (true)
        {
          do
            while (true)
            {
              while (true)
              {
                ++k;
                j = i1;
                l = i2;
                localObject3 = localObject6;
                localObject4 = localObject5;
              }
              String str3 = str2.substring(0, str2.indexOf(";"));
              String str4 = str2.substring(1 + str3.length());
              localObject1 = str3;
              localObject2 = str4;
            }
          while (c1 != ';');
          localObject5 = localObject4;
          localObject6 = localObject3;
          i1 = 0;
          i2 = l;
        }
        if (c1 != '=')
          break;
        log.error("Expected header param name, got '='");
        localObject5 = localObject4;
        localObject6 = localObject3;
        i1 = 99;
        i2 = l;
      }
      localObject5 = new CharArrayBuffer(64);
      localObject6 = new CharArrayBuffer(64);
      i1 = 1;
    case 2:
    case 3:
    case 4:
    case 5:
    case 1:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            if (c1 != '=')
              break label444;
            if (((CharArrayBuffer)localObject5).length() != 0)
              break;
            i1 = 99;
            i2 = l;
          }
          i1 = 2;
          i2 = l;
        }
        label444: ((CharArrayBuffer)localObject5).append(c1);
        i2 = l;
      }
      int i6 = 0;
      switch (c1)
      {
      default:
        j = 3;
        i6 = 1;
      case '\t':
      case ' ':
      case '"':
      }
      while (true)
      {
        while (true)
        {
          if (i6 != 0)
            break label532;
          i2 = l;
          localObject5 = localObject4;
          localObject6 = localObject3;
          i1 = j;
        }
        j = 4;
      }
      label532: int i5 = 0;
      switch (c1)
      {
      default:
        ((CharArrayBuffer)localObject3).append(c1);
      case '\t':
      case ' ':
      case ';':
      }
      while (true)
      {
        while (true)
        {
          if (i5 != 0)
            break label639;
          i2 = l;
          localObject5 = localObject4;
          localObject6 = localObject3;
          i1 = j;
        }
        localHashMap.put(((CharArrayBuffer)localObject4).toString().trim().toLowerCase(), ((CharArrayBuffer)localObject3).toString().trim());
        j = 5;
        i5 = 1;
      }
      label639: int i3 = j;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                    switch (c1)
                    {
                    default:
                      localObject5 = localObject4;
                      localObject6 = localObject3;
                      i1 = 99;
                      i2 = l;
                    case ';':
                    case '\t':
                    case ' ':
                    }
                  localObject5 = localObject4;
                  localObject6 = localObject3;
                  i1 = 0;
                  i2 = l;
                }
                i2 = l;
                int i4 = i3;
                localObject5 = localObject4;
                localObject6 = localObject3;
                i1 = i4;
              }
              switch (c1)
              {
              default:
                if (l != 0)
                  ((CharArrayBuffer)localObject3).append('\\');
                l = 0;
                ((CharArrayBuffer)localObject3).append(c1);
              case '"':
              case '\\':
              }
            }
            if (l != 0)
              break;
            localHashMap.put(((CharArrayBuffer)localObject4).toString().trim().toLowerCase(), ((CharArrayBuffer)localObject3).toString());
            localObject5 = localObject4;
            localObject6 = localObject3;
            i1 = 5;
            i2 = l;
          }
          ((CharArrayBuffer)localObject3).append(c1);
          i2 = 0;
          localObject5 = localObject4;
          localObject6 = localObject3;
          i1 = j;
        }
        if (l != 0)
          ((CharArrayBuffer)localObject3).append('\\');
        if (l == 0)
          i2 = 1;
        while (true)
        {
          while (true)
          {
            localObject5 = localObject4;
            localObject6 = localObject3;
            i1 = j;
          }
          i2 = 0;
        }
        if (j == 3)
          label906: localHashMap.put(((CharArrayBuffer)localObject4).toString().trim().toLowerCase(), ((CharArrayBuffer)localObject3).toString().trim());
        label939: return localHashMap;
        i3 = j;
      }
      localObject5 = localObject4;
      localObject6 = localObject3;
      i1 = j;
    }
  }

  public static boolean isBase64Encoding(String paramString)
  {
    return "base64".equalsIgnoreCase(paramString);
  }

  public static boolean isMessage(String paramString)
  {
    int i;
    if ((paramString != null) && (paramString.equalsIgnoreCase("message/rfc822")))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public static boolean isMultipart(String paramString)
  {
    int i;
    if ((paramString != null) && (paramString.toLowerCase().startsWith("multipart/")))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public static boolean isQuotedPrintableEncoded(String paramString)
  {
    return "quoted-printable".equals(paramString);
  }

  public static boolean isSameMimeType(String paramString1, String paramString2)
  {
    int i;
    if ((paramString1 != null) && (paramString2 != null) && (paramString1.equalsIgnoreCase(paramString2)))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  /**
   * @deprecated
   */
  private static int nextCounterValue()
  {
    monitorenter;
    try
    {
      int i = counter;
      counter = i + 1;
      monitorexit;
      return i;
    }
    finally
    {
      localObject = finally;
      monitorexit;
      throw localObject;
    }
  }
}